package com.jzy.weblog.admin.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jzy.weblog.admin.model.vo.tag.*;
import com.jzy.weblog.admin.service.AdminTagService;
import com.jzy.weblog.common.domain.dos.TagDO;
import com.jzy.weblog.common.domain.mapper.TagMapper;
import com.jzy.weblog.common.enums.ResponseCodeEnum;
import com.jzy.weblog.common.model.vo.SelectRspVO;
import com.jzy.weblog.common.utils.PageResponse;
import com.jzy.weblog.common.utils.Response;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
public class AdminTagServiceImpl extends ServiceImpl<TagMapper, TagDO> implements AdminTagService {

    @Autowired
    private TagMapper tagMapper;

    /**
     * 添加标签集合
     * @param addTagReqVO
     * @return
     */
    @Override
    public Response addTags(AddTagReqVO addTagReqVO) {
        // VO 转 DO
        List<TagDO> tagDOS = addTagReqVO.getTags()
                        .stream().map(tagName -> TagDO.builder()
                            .name(tagName.trim()) //去掉前后空格
                            .createTime(LocalDateTime.now())
                            .updateTime(LocalDateTime.now())
                            .build())
                        .collect(Collectors.toList());

        try{
            // 批量插入
            saveBatch(tagDOS);
        }catch (Exception e){
            log.warn("该标签已经存在", e);
        }

        return Response.success();
    }

    /**
     * 查询标签分页
     * @param findTagPageListReqVO
     * @return
     */
    @Override
    public PageResponse findTagPageList(FindTagPageListReqVO findTagPageListReqVO) {
        // 分页参数，条件参数
        Long current = findTagPageListReqVO.getCurrent();
        Long size = findTagPageListReqVO.getSize();
        String name = findTagPageListReqVO.getName();
        LocalDate startDate = findTagPageListReqVO.getStartDate();
        LocalDate endDate = findTagPageListReqVO.getEndDate();

        //分页查询
        Page<TagDO> page = tagMapper.selectPageList(current, size, name, startDate, endDate);
        List<TagDO> records = page.getRecords();

        // DO转VO
        List<FindTagPageListRspVO> vos = null;
        if(!CollectionUtils.isEmpty(records)){
            vos = records.stream().map(tagDO-> FindTagPageListRspVO.builder()
                    .id(tagDO.getId())
                    .name(tagDO.getName())
                    .createTime(tagDO.getCreateTime())
                    .build()).collect(Collectors.toList());
        }

        return PageResponse.success(page,vos);
    }

    @Override
    public Response deleteTag(DeleteTagReqVO deleteTagReqVO) {
        long tagId = deleteTagReqVO.getId();
        // 删除标签
        int count = tagMapper.deleteById(tagId);

        return count == 1? Response.success() : Response.fail(ResponseCodeEnum.TAG_NOT_EXISTED);
    }

    @Override
    public Response searchTag(SearchTagReqVO searchTagReqVO) {
        String key = searchTagReqVO.getKey();

        //执行模糊查询
        List<TagDO> tagDOS = tagMapper.selectByKey(key);

        // DO 转 VO
        List<SelectRspVO> vos = null;
        if(!CollectionUtils.isEmpty(tagDOS)){
            vos = tagDOS.stream()
                    .map(tagDO -> SelectRspVO.builder()
                        .label(tagDO.getName())
                        .value(tagDO.getId())
                        .build())
                    .collect(Collectors.toList());
        }

        return Response.success(vos);
    }
}
